package com.settlement.system.service;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.SinglePage;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.dao.DuplicateKeyException;

import java.util.List;
import java.util.Map;

/**
 * @author author
 */
public interface IBaseService {
    /**
     * 插入一条数据
     *
     * @param modelType
     * @return Integer
     * @throws ServiceException
     */
    public abstract <ModelType> Integer insert(ModelType modelType) throws ServiceException;

    /**
     * 更新数据
     *
     * @param modelType
     * @return Integer
     * @throws ServiceException
     */
    public abstract <ModelType> Integer update(ModelType modelType) throws ServiceException;

    /**
     * 删除数据
     *
     * @param modelType
     * @return Integer
     * @throws ServiceException
     */
    public abstract <ModelType> Integer deleteById(ModelType modelType) throws ServiceException;

    Integer deleteByParams(Map<String,Object> params) throws ServiceException;

    /**
     * 获取数量
     *
     * @param params
     * @return
     * @throws ServiceException
     */
    public abstract Integer selectCount(Map<String, Object> params) throws ServiceException;

    /**
     * 获取分页列表
     *
     * @param page
     * @param params
     * @param orderBy
     * @param orderByField
     * @return
     * @throws ServiceException
     */
    public abstract <ModelType> List<ModelType> selectByPage(SinglePage page, Map<String, Object> params, String orderByField, String orderBy) throws ServiceException;

    /**
     * 根据参数获取列表
     *
     * @param params
     * @return
     * @throws ServiceException
     */
    public abstract <ModelType> List<ModelType> selectByParam(Map<String, Object> params) throws ServiceException;

    /**
     * 保持一个列表
     *
     * @param list
     * @param <ModelType>
     * @throws ServiceException
     */
    public abstract <ModelType> void save(List<ModelType> list) throws ServiceException;

    /**
     * 通过handler获取数据
     *
     * @param params
     * @param handler
     * @param <ModelType>
     * @throws ServiceException
     */
    public abstract <ModelType> void getDataByHandler(Map<String, Object> params, ResultHandler<ModelType> handler) throws ServiceException;

    public abstract void getMapDataByHandler(Map<String, Object> params, ResultHandler<Map<String, Object>> handler) throws ServiceException;

    /**
     * 通过handler获取数据
     *
     * @param params
     * @param handler
     * @param <ModelType>
     * @throws ServiceException
     */
    public abstract <ModelType> void getPageDataByHandler(Map<String, Object> params, ResultHandler<ModelType> handler) throws ServiceException;

    /**
     * 通过id,批量删除明细
     *
     * @param list 数据列表
     * @return 删除的数量
     * @throws ServiceException
     */
    public abstract <ModelType> Integer batchDelete(List<ModelType> list) throws ServiceException;

    /**
     * 保持一个不重复的数据列表,当重复的时候,抛出DuplicateKeyException异常,
     * 可以通过DuplicateKeyException.getMessage 获取重复的数据
     * <p>
     * only return frist duplicate data when list exist many of duplicate data
     *
     * @param list
     * @param <ModelType>
     * @return number of save data
     * @throws ServiceException
     * @throws DuplicateKeyException
     */
    public abstract <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException;

    /**
     * 根据model获取单条
     * @param modelType
     * @return
     */
    public abstract <ModelType> ModelType selectSingleByModel(ModelType modelType) throws ServiceException;
}
